package com.util.thread;

/**
 * @author xin.ding
 * @date 2020/10/31 16:17
 */
public class StopThreadByException {

    public static void main(String[] args) {
        try {
            StopThreadByExceptionAEx stopThreadByExceptionEx = new StopThreadByExceptionAEx();
            stopThreadByExceptionEx.start();
            Thread.sleep(10);
            stopThreadByExceptionEx.interrupt();
        } catch (Exception e) {
            System.out.println("main catch!!!");
            e.printStackTrace();
        }
        System.out.println("end!!!");
    }
}

class StopThreadByExceptionAEx extends Thread {
    @Override
    public void run() {
        try {
            for (int i = 0; i < 100; i++) {
                //declare current thread is stop status
                if (interrupted()) {
                    System.out.println("current thread is stop status,i will exit!!");
                    throw new InterruptedException();
                }
                System.out.println("i" + (i + 1));
            }
            System.out.println("i am blew at for");
        } catch (InterruptedException e) {
            System.out.println("go to the catch in the Thread.java class run method");
            e.printStackTrace();
        }
    }

}
